<template>
  <div class="com-container" @dblclick="revertMap">

    <div class="com-chart" ref="dv"></div>
  </div>
</template>

<script>
import axios from 'axios'
import {
  getProvinceMapInfo
} from '../utils/map_utils'

export default {
  data () {
    return {
      chartInstance: '',
      allData: []
    }
  },
  mounted () {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.getScatterData()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    async initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.dv, 'chalk')
      const {
        data: mapData
      } = await
      axios.get('./static/map/china.json')
      this.$echarts.registerMap('china', mapData)
      const initOption = {
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2E72BF',
            borderColor: '#333'
          }
        },
        title: {
          text: '▎ 商家分布',
          left: 20,
          top: 20
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      }
      this.chartInstance.on('click', arg => {
      })
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('click', async arg => {
        const provinceInfo = getProvinceMapInfo(arg.name)
        const {
          data: ret
        } = await axios.get('./' + provinceInfo.path)
        console.log(provinceInfo)
        this.$echarts.registerMap(provinceInfo.key, ret)
        this.chartInstance.setOption({
          geo: {
            map: provinceInfo.key
          }
        })
      })
      this.chartInstance.setOption(initOption)
    },
    revertMap () {
      this.chartInstance.setOption({
        geo: {
          map: 'china'
        }
      })
    },
    async getScatterData () {
      const {
        data: ret
      } = await this.$http.get('./data/map.json')
      this.allData = ret
      this.updateChart()
    },
    async getData () {
      this.updateChart()
    },
    updateChart () {
      const legendData = this.allData.map(item => {
        return item.name
      })
      const seriesArr = this.allData.map(item => {
        return {
          type: 'effectScatter',
          coordinateSystem: 'geo',
          name: item.name,
          data: item.children,
          rippleEffect: {
            scale: 20,
            brushType: 'stroke'
          }
        }
      })
      const dataOption = {
        legend: {
          data: legendData
        },
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter () {
      const titleFontSize = this.$refs.dv.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    }
  }
}
</script>

<style>
</style>
